/**
 *********************************************************
 ** <h1>SFMLex - Simple and Fast Multimedia Library Extensions.</h1>
 ** SFMLex Copyright (C) 2008 Neil Johnson (neil@emailus.me.uk)
 **
 ** SFML - Simple and Fast Multimedia Library
 ** Copyright (C) 2007-2008 Laurent Gomila (laurent.gom@gmail.com)
 **
 ** This software is provided 'as-is', without any express or implied warranty.
 ** In no event will the authors be held liable for any damages arising from the use of this software.
 **
 ** Permission is granted to anyone to use this software for any purpose,
 ** including commercial applications, and to alter it and redistribute it freely,
 ** subject to the following restrictions:
 **
 ** 1. The origin of this software must not be misrepresented;
 **    you must not claim that you wrote the original software.
 **    If you use this software in a product, an acknowledgment
 **    in the product documentation would be appreciated but is not required.
 **
 ** 2. Altered source versions must be plainly marked as such,
 **    and must not be misrepresented as being the original software.
 **
 ** 3. This notice may not be removed or altered from any source distribution.
 **
 ************************************************************
 */
#include <sstream>
#include <iostream>
#include <assert.h>

#include "c_SpriteResource.h"
#include "c_ResourceMgr.h"

namespace sfx
{
   //
   // c_SpriteResource
   //-----------------
   c_SpriteResource::c_SpriteResource( c_ResourceMgr* ResourceMgr ) :
   
   c_Resource( ResourceMgr ),

   m_ImageName_Set(false),
   m_SubRec_Set(false),
   m_FlipX_Set( false ),
   m_FlipY_Set( false ),
   m_Position_Set( false ),
   m_Centre_Set( false ),
   m_Colour_Set( false ),
   m_BlendMode_Set( false ),
   m_Rotation_Set( false ),
   m_Sprites()
   {
      std::cerr << "c_SpriteResource()\n";
      
      m_Map.insert( make_pair("image", t_SetResouceFunc( boost::bind( &c_SpriteResource::setImageName, boost::ref(*this), _1 ) ) ) );
      m_Map.insert( make_pair("subrect", t_SetResouceFunc( boost::bind( &c_SpriteResource::setSubRect, boost::ref(*this), _1 ) ) ) );
      m_Map.insert( make_pair("flipx", t_SetResouceFunc( boost::bind( &c_SpriteResource::setFlipX, boost::ref(*this), _1 ) ) ) );
      m_Map.insert( make_pair("flipy", t_SetResouceFunc( boost::bind( &c_SpriteResource::setFlipY, boost::ref(*this), _1 ) ) ) );
      m_Map.insert( make_pair("position", t_SetResouceFunc( boost::bind( &c_SpriteResource::setPosistion, boost::ref(*this), _1 ) ) ) );
      m_Map.insert( make_pair("centre", t_SetResouceFunc( boost::bind( &c_SpriteResource::setCentre, boost::ref(*this), _1 ) ) ) );
      m_Map.insert( make_pair("rotation", t_SetResouceFunc( boost::bind( &c_SpriteResource::setRotation, boost::ref(*this), _1 ) ) ) );
      m_Map.insert( make_pair("colour", t_SetResouceFunc( boost::bind( &c_SpriteResource::setColour, boost::ref(*this), _1 ) ) ) );
      m_Map.insert( make_pair("blendmode", t_SetResouceFunc( boost::bind( &c_SpriteResource::setBlendMode, boost::ref(*this), _1 ) ) ) );
      
   }

   void c_SpriteResource::setSubRect( const std::string& SubRect )
   {
      std::stringstream ss( SubRect, std::stringstream::in );
      
      ss >> m_SubRec.Left;
      ss.ignore( 1, ',' );
      ss >> m_SubRec.Top;
      ss.ignore( 1, ',' );
      ss >> m_SubRec.Right;
      ss.ignore( 1, ',' );
      ss >> m_SubRec.Bottom;

      //std::cout << m_SubRec.Left << "," << m_SubRec.Top << "," << m_SubRec.Right << "," << m_SubRec.Bottom << std::endl;
      m_SubRec_Set = true;
   }
   
   sf::Sprite* c_SpriteResource::createSprite( void )
   {
      sf::Sprite* Sprite;
      Sprite = new sf::Sprite();
      assert( Sprite != 0 );
      if( Sprite )
      {
         if( m_ImageName_Set )
         {
            sf::Image* Image;
            Image = m_ResourceMgr->getImage( m_ImageName );
            if( Image )
            {
               assert( Image != 0 );
               Sprite->SetImage( *Image );
            }
         }
         
         if( m_SubRec_Set )
         {
            Sprite->SetSubRect( m_SubRec );
         }
         
         if( m_FlipX_Set )
         {
            Sprite->FlipX( m_FlipX );
         }

         if( m_FlipY_Set )
         {
            Sprite->FlipY( m_FlipY );
         } 
         
         if( m_Position_Set )
         {
            Sprite->SetPosition( m_Position );
         }

         if( m_Centre_Set )
         {
            Sprite->SetOrigin( m_Centre );
         }

         if( m_Colour_Set )
         {
            Sprite->SetColor( m_Colour );
         }

         if( m_BlendMode_Set )
         {
            Sprite->SetBlendMode( m_BlendMode );
         }
         
         if( m_Rotation_Set )
         {
            Sprite->SetRotation( m_Rotation );
         }

         m_Sprites.push_back( Sprite );
      }
      return Sprite;
   }
   
   bool c_SpriteResource::freeSprite( sf::Sprite* DeadSprite )
   {
      std::list<sf::Sprite*>::iterator it;

      for( it = m_Sprites.begin(); it != m_Sprites.end(); ++it )
      {
         sf::Sprite* Sprite;
         Sprite = *it;
         if( Sprite == DeadSprite )
         {
            m_Sprites.erase( it );
            delete Sprite;
            return true;
         }
      }

      return false;
   }

   bool c_SpriteResource::purge( void )
   {
      std::list<sf::Sprite*>::iterator it;

      for( it = m_Sprites.begin(); it != m_Sprites.end(); ++it )
      {
         sf::Sprite* Sprite;
         Sprite = *it;
         
         std::cerr << "c_SpriteResource::purge( " << (void*)Sprite << " )\n";

         delete Sprite;
      }
      m_Sprites.clear();

      return true;
   }
   

   void c_SpriteResource::setFlipX( const std::string& FlipX )
   {
      if( FlipX == "true" )
      {
         m_FlipX = true;
      }
      else
      {
         m_FlipX = false;
      }

      m_FlipX_Set = true;
   }

   void c_SpriteResource::setFlipY( const std::string& FlipY )
   {
      if( FlipY == "true" )
      {
         m_FlipY = true;
      }
      else
      {
         m_FlipY = false;
      }

      m_FlipY_Set = true;
   }
   
   void c_SpriteResource::setPosistion( const std::string& Position )
   {
      std::stringstream ss( Position, std::stringstream::in );
      
      ss >> m_Position.x;
      ss.ignore( 1, ',' );
      ss >> m_Position.y;

      m_Position_Set = true;
   }
   
   void c_SpriteResource::setCentre( const std::string& Centre )
   {
      std::stringstream ss( Centre, std::stringstream::in );
      
      ss >> m_Centre.x;
      ss.ignore( 1, ',' );
      ss >> m_Centre.y;

      m_Centre_Set = true;
   }

   void c_SpriteResource::setRotation( const std::string& Rotation )
   {
      std::stringstream ss( Rotation, std::stringstream::in );

      ss >> m_Rotation;

      m_Rotation_Set = true;
   }
   
   void c_SpriteResource::setColour( const std::string& Colour )
   {
      std::stringstream ss( Colour, std::stringstream::in );
      
      int Tmp;
      ss >> Tmp ;
      m_Colour.r = Tmp;
      ss.ignore( 1, ',' );
      ss >> Tmp;
      m_Colour.g = Tmp;
      ss.ignore( 1, ',' );
      ss >> Tmp;
      m_Colour.b = Tmp;
      ss.ignore( 1, ',' );
      ss >> Tmp;
      m_Colour.a = Tmp;

      //std::cerr << (int)m_Colour.r << "," << (int)m_Colour.g << "," << (int)m_Colour.b << "," << (int)m_Colour.a << std::endl;
  
      m_Colour_Set = true;
   }
   
   void c_SpriteResource::setBlendMode( const std::string& BlendMode )
   {
      if( BlendMode == "alpha" )
      {
         m_BlendMode = sf::Blend::Alpha;
      }
      else if( BlendMode == "add" )
      {
         m_BlendMode = sf::Blend::Add;
      }
      else if( BlendMode == "multiply" )
      {
         m_BlendMode = sf::Blend::Multiply;
      }
      else
      {
         m_BlendMode = sf::Blend::None;
      }

      m_BlendMode_Set = true;
   }
   

}
